<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
        <el-form-item label="任务编号" prop="rrTaskCode">
          <el-input v-model="queryParams.rrTaskCode" clearable placeholder="请输入任务编号" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="任务名称" prop="rrTaskName">
          <el-input v-model="queryParams.rrTaskName" clearable placeholder="请输入任务名称" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="工单编码" prop="rrWorkorderCode">
          <el-input v-model="queryParams.rrWorkorderCode" clearable placeholder="请输入工单编码" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="工序名称" prop="processName">
          <el-input v-model="queryParams.processName" clearable placeholder="请输入工序名称" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="任务状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择任务状态" size="small">
            <el-option v-for="dict in dict.type.mes_task_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-table v-loading="loading" :data="rrTaskList" border @current-change="handleCurrent">
        <el-table-column align="center" label="序号" type="index" />
        <el-table-column align="center" label="任务编号" prop="rrTaskCode" width="120px">
          <template slot-scope="scope">
            <el-button v-hasPermi="['mes:pro:feedback:query']" size="mini" type="text" @click="handleView(scope.row)">
              {{ scope.row.rrTaskCode }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" align="center" label="任务名称" prop="rrTaskName" width="180px" />
        <el-table-column :show-overflow-tooltip="true" align="center" label="工单编号" prop="rrWorkorderCode" width="150px" />
        <el-table-column :show-overflow-tooltip="true" align="center" label="工作站名称" prop="workstationName" width="150px" />
        <el-table-column :show-overflow-tooltip="true" align="center" label="工序名称" prop="processName" />
        <el-table-column :show-overflow-tooltip="true" align="center" label="工序编号" prop="processCode" width="180" />
        <el-table-column align="center" label="任务颜色" prop="colorCode">
          <template slot-scope="scope">
            <el-color-picker v-model="scope.row.colorCode" disabled></el-color-picker>
          </template>
        </el-table-column>
        <el-table-column align="center" label="计划开工时间" prop="startTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="计划完工时间" prop="endTime " width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" align="center" label="计划生产时长" prop="duration" width="100" />
        <el-table-column align="center" label="实际开工时间" prop="actualStartTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.actualStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="实际完工时间" prop="actualEndTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.actualEndTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" align="center" label="实际生产时长" prop="actualDuration" width="100" />
        <el-table-column align="center" fixed="right" label="排产数量" prop="quantity" width="100">
          <template slot-scope="scope">
            <el-tag type="danger">{{ scope.row.quantity }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="合格品数量" prop="quantityQuanlify" width="100">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.quantityQuanlify }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="状态" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mes_task_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="280px">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 'PROGRESS'" v-hasPermi="['mes:pro:protask:start']" icon="el-icon-close" size="mini" type="text" @click="handleCancel(scope.row)">
              取消
            </el-button>
            <el-button v-if="scope.row.status == 'PROGRESS'" icon="el-icon-plus" size="mini" type="text" @click="handleAddFeedback(scope.row)">报工</el-button>
            <el-button v-if="scope.row.status == 'PENDUING'" icon="el-icon-edit" size="mini" type="text" @click="handleStart(scope.row)">开工</el-button>
            <el-button v-if="scope.row.status == 'PROGRESS'" icon="el-icon-success" size="mini" type="text" @click="handleFinished(scope.row)">完工</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList" />
    </el-card>
    <TaskView v-if="infoOpen" ref="taskView" :optType="optType"></TaskView>
    <FeedBack v-if="feedBackOpen" ref="feedBack" @cancel="cancel"></FeedBack>
  </div>
</template>
<script>
import { finish, start, listTask, cancel } from '@/api/mes/rr/rrtask'
import UserSingleSelect from '@/components/userSelect/single.vue'
import TaskView from '@/views/mes/rr/returnrepairtask/taskView.vue'
import FeedBack from '@/views/mes/rr/returnrepairtask/feedBack.vue'

export default {
  name: 'proTask',
  components: { FeedBack, TaskView, UserSingleSelect },
  dicts: ['mes_task_status', 'mes_feedback_type'],
  data() {
    return {
      printOpen: false,
      autoGenFlag: false,
      feedBackOpen: false,
      optType: undefined,
      title: '',
      outsourceTitle: '',
      open: false,
      outsourceOpen: false,
      shen: false,
      infoOpen: false,
      loading: true,
      selectedTaskId: null,
      selectedRows: null,
      showSearch: true,
      total: 0,
      rrTaskList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rrTaskCode: null,
        rrTaskName: null,
        rrWorkorderCode: null,
        processName: null,
        status: null
      },
      form: {},
      rules: {
        feedbackType: [{ required: true, message: '报工类型不能为空', trigger: 'change' }],
        rrTaskCode: [{ required: true, message: '请选择生产任务', trigger: 'blur' }],
        rrWorkorderCode: [{ required: true, message: '生产工单不能为空', trigger: 'blur' }],
        nickName: [{ required: true, message: '请选择报工人', trigger: 'blur' }],
        feedbackTime: [{ required: true, message: '请选择报工时间', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询生产报工记录列表 */
    getList() {
      this.loading = true
      this.selectedTaskId = undefined
      listTask(this.queryParams).then((response) => {
        this.rrTaskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleCurrent(row) {
      if (row) {
        this.selectedTaskId = row.rrTaskId
        this.selectedRows = row
      }
    },
    /* 单选选中数据 */
    handleRowChange(row) {
      this.selectedRows = row
    },
    /* 取消按钮 */
    cancel() {
      this.open = false
      this.shen = false
      this.getList()
    },
    /* 表单重置 */
    reset() {
      this.form = {
        recordId: null,
        feedbackType: null,
        quantity: 0,
        quantityUncheck: 0,
        quantityFeedback: 0,
        quantityQualified: 0,
        quantityUnquanlified: 0,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        feedbackTime: new Date(),
        recordUser: null,
        recordNick: null,
        status: 'PREPARE',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /* 查询明细按钮操作 */
    handleView(row) {
      const rrTaskId = row.rrTaskId
      this.infoOpen = true
      this.optType = 'view'
      this.$nextTick(() => this.$refs.taskView.init(rrTaskId))
    },
    /* 查询明细按钮操作 */
    handleFinished(row) {
      const rrTaskId = row.rrTaskId
      this.infoOpen = true
      this.optType = 'save'
      this.$nextTick(() => this.$refs.taskView.init(rrTaskId))
    },
    /*查看任务详情*/
    handleInfoView(row) {
      this.$router.push({
        path: '/taskInfo',
        query: { rrTaskId: row.rrTaskId }
      })
    },
    /*开工*/
    handleStart(row) {
      this.$modal
        .confirm('开工后任务将进入生产中状态，是否继续？')
        .then(function () {
          return start({ rrTaskId: row.rrTaskId })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('开工成功')
        })
        .catch(() => {})
    },
    /*完工*/
    handleEnd(row) {
      this.$modal
        .confirm('任务完工后将不可报工，是否确认完工？')
        .then(function () {
          return finish({ rrTaskId: row.rrTaskId })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('完工成功')
        })
        .catch(() => {})
    },
    /*报工*/
    handleAddFeedback(row) {
      this.reset()
      this.feedBackOpen = true
      this.title = '生产报工'
      this.$nextTick(() => this.$refs.feedBack.init(row.rrTaskId))
    },
    //取消开工
    handleCancel(row) {
      this.$modal
        .confirm('是否确认取消开工？')
        .then(function () {
          return cancel({ rrTaskId: row.rrTaskId })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('操作成功')
        })
        .catch(() => {})
    }
  }
}
</script>
